<?php

namespace app\member\controller\api;

use app\ApiController;

use app\member\model\UserAddressModel;

use think\Db;


/*------------------------------------------------------ */
//-- 会员相关API
/*------------------------------------------------------ */

class Address extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new UserAddressModel();
    }
    /*------------------------------------------------------ */
    //-- 获取地址列表
    /*------------------------------------------------------ */
    public function getList()
    {
        $list = $this->Model->getRows();
        foreach ($list as $row) {
            $return['list'][] = $row;
        }
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 设置默认地址
    /*------------------------------------------------------ */
    public function editDefault()
    {
        $address_id = input('address_id', 0, 'intval');
        if ($address_id < 1) return $this->error('传参失败，请重试.');
        $where[] = ['user_id', '=', $this->userInfo['user_id']];
        $where[] = ['address_id', '=', $address_id];
        $res = $this->Model->where($where)->update(['is_default' => 1]);
        if ($res > 0) {
            unset($where);
            $where[] = ['user_id', '=', $this->userInfo['user_id']];
            $where[] = ['address_id', '<>', $address_id];
            $this->Model->where($where)->update(['is_default' => 0]);
            $this->Model->cleanMemcache();
        }
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 添加地址
    /*------------------------------------------------------ */
    public function add()
    {
        $inarr = $this->checkData();
        $inarr['user_id'] = $this->userInfo['user_id'];
        unset($inarr['address_id']);
        $res = $this->Model->save($inarr);
        if ($res < 1) return $this->error('添加失败，请重试..');
        $this->Model->cleanMemcache();
        return $this->getList();
    }
    /*------------------------------------------------------ */
    //-- 修改地址
    /*------------------------------------------------------ */
    public function edit()
    {
        $uparr = $this->checkData();
        $where['address_id'] = $uparr['address_id'];
        $where['user_id'] = $this->userInfo['user_id'];
        unset($uparr['address_id']);
        $res = $this->Model->where($where)->update($uparr);
        if ($res < 1) return $this->error('修改失败，请重试..');
        $this->Model->cleanMemcache();
        return $this->getList();
    }

    /*------------------------------------------------------ */
    //-- 验证修改/添加数据
    /*------------------------------------------------------ */
    private function checkData()
    {
        $data['consignee'] = input('consignee', '', 'trim');
        $data['address'] = input('address', '', 'trim');
        $data['address_id'] = input('address_id', 0, 'intval');
        $data['mobile'] = input('mobile', '', 'trim');
        if (empty($data['consignee'])) return $this->error('请输入收货人.');
        if (empty($data['address'])) return $this->error('请输入详细地址.');
        if (empty($data['mobile'])) return $this->error('请输入手机号码.');
        if (checkMobile($data['mobile']) == false) return $this->error('手机号码格式不正确.');
        $regionIds = input('regionIds', '', 'trim');
        if (empty($regionIds)) return $this->error('请选择所在地区.');
        $regionIds = explode(',', $regionIds);
        $data['province'] = $regionIds[0];
        $data['city'] = $regionIds[1];
        $data['district'] = $regionIds[2];
        $data['street'] = $regionIds[3];
        if ($data['district'] < 1) return $this->error('请选择所在地区.');

        $name = Db::table('bs_province')->where('PROVINCE_CODE',$regionIds[0])->value('PROVINCE_NAME');
        $name = $name.','.Db::table('bs_city')->where('CITY_CODE',$regionIds[1])->value('CITY_NAME');
        $name = $name.','.Db::table('bs_area')->where('AREA_CODE',$regionIds[2])->value('AREA_NAME');
        $name = $name.','.Db::table('bs_street')->where('STREET_CODE',$regionIds[3])->value('STREET_NAME');
        $data['merger_name'] = $name;
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 删除地址
    /*------------------------------------------------------ */
    public function delete()
    {
        $where['user_id'] = $this->userInfo['user_id'];
        $count = $this->Model->where($where)->count();
        if ($count ==1)return $this->error('必须保留一个收货地址...');
        $where['address_id'] = input('address_id', 0, 'intval');
        $res = $this->Model->where($where)->delete();
        if ($res < 1) return $this->error('删除失败，请重试..');
        $this->Model->cleanMemcache();
        return $this->getList();
    }

    /*------------------------------------------------------ */
    //-- 获取地区
    /*------------------------------------------------------ */
    // public function getRegion(){
    //     $parent_id = I('get.parent_id/d');
    //     $selected = I('get.selected',0);
    //     $data = Db::name('region')->where("id",$parent_id)->select();
    //     $html = '';
    //     if($data){
    //         foreach($data as $h){
    //             if($h['id'] == $selected){
    //                 $html .= "<option value='{$h['id']}' selected>{$h['name']}</option>";
    //             }
    //             $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
    //         }
    //     }
    //     echo $html;
    // }
    public function getRegion(){
        $parent_id = input('parent_id',0,'string');
        $selected = input('selected',1,'intval');
        switch($selected){
            case 1:
                $return['data'] = Db::table('bs_province')->field('PROVINCE_CODE as code,PROVINCE_NAME as name')->select();
                break;
            case 2:
                $return['data'] = Db::table('bs_city')->where('PROVINCE_CODE',$parent_id)->field('CITY_CODE as code,CITY_NAME as name')->select();
                break;
            case 3:
                $return['data'] = Db::table('bs_area')->where('CITY_CODE',$parent_id)->field('AREA_CODE as code,AREA_NAME as name')->select();
                break;
            case 4:
                $return['data'] = Db::table('bs_street')->where('AREA_CODE',$parent_id)->field('STREET_CODE as code,STREET_NAME as name')->select();
                break;
            default:
            $return['data'] = '';
        }
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
}
